package com.yunqing.restkeeper.store.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqing.restkeeper.common.exception.BussinessException;
import com.yunqing.restkeeper.store.api.entity.DishCategory;
import com.yunqing.restkeeper.store.api.service.IDishCategoryService;
import com.yunqing.restkeeper.store.mapper.DishCategoryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DishCategoryServiceImpl
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/7/23
 */
@Service("dishCategoryService")
@DubboService(version = "1.0.0", protocol = "dubbo")
public class DishCategoryServiceImpl extends ServiceImpl<DishCategoryMapper, DishCategory> implements IDishCategoryService {
    @Override
    @Transactional
    public boolean add(String name, int type) {

        //判断当前的分类名称在表中是否存在
        this.checkNameExist(name);

        DishCategory dishCategory = new DishCategory();
        dishCategory.setName(name);
        dishCategory.setType(type);
        dishCategory.setTorder(0);

        return this.save(dishCategory);
    }

    @Override
    @Transactional
    public boolean update(String id, String name) {

        //判断当前的分类名称在表中是否存在
        this.checkNameExist(name);

        UpdateWrapper<DishCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(DishCategory::getName, name).eq(DishCategory::getCategoryId, id);

        return this.update(updateWrapper);
    }

    @Override
    public IPage<DishCategory> queryPage(int pageNum, int pageSize) {

        IPage<DishCategory> page = new Page<>(pageNum, pageSize);

        QueryWrapper<DishCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(DishCategory::getLastUpdateTime);

        return this.page(page, queryWrapper);
    }

    @Override
    public List<Map<String, Object>> findCategoryList(Integer type) {

        QueryWrapper<DishCategory> queryWrapper = new QueryWrapper<>();

        if (type != null) {
            queryWrapper.lambda().eq(DishCategory::getType, type);
        }

        queryWrapper.lambda().select(DishCategory::getCategoryId, DishCategory::getName);

        return this.listMaps(queryWrapper);
    }

    private void checkNameExist(String name) {
        QueryWrapper<DishCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(DishCategory::getCategoryId).eq(DishCategory::getName, name);

        Long count = this.getBaseMapper().selectCount(queryWrapper);
        if (count > 0) throw new BussinessException("该分类已经存在");
    }
}
